#!/bin/bash
# 定义函数
function install() {
    echo "[          ]将默认软件源替换为腾讯软件源"
    sed -i 's@^\(deb.*stable main\)$@#\1\ndeb https://mirrors.cloud.tencent.com/termux/apt/termux-main stable main@' $PREFIX/etc/apt/sources.list
    echo "[▆         ]更新源列表..."
    pkg update &>/dev/null
    echo "[▆▆▆       ]安装交互界面..."
    pkg install -y ncurses libevent &>/dev/null
    yes | pkg install whiptail &>/dev/null
    echo "[▆▆▆▆      ]安装终端复用器..."
    pkg install tmux &>/dev/null
    echo "[▆▆▆▆▆     ]安装Alist..."
    pkg install alist &>/dev/null
    echo "[▆▆▆▆▆▆▆   ]添加常用脚本..."
    (printf 'tmux kill-window -t "alist_window" ; sleep 1 ; tmux new-window -n alist_window ; tmux send-keys -t main:alist_window "alist server" C-m ; tmux select-window -t main:gui; tmux send-keys -t main:gui "clear ; echo "Alist正在启动..." ; sleep 1 ; ~/.starting.sh ; sleep 3 ; ~/.info.sh" C-m || ~/.bashrc' > ~/.restart.sh ; chmod +x ~/.restart.sh) &>/dev/null
    (printf 'TERM=ansi whiptail --infobox "Alist正在启动..." 8 40' > ~/.starting.sh ; chmod +x ~/.starting.sh) &>/dev/null
    (printf 'termux-open-url http://127.0.0.1:5244 ; TERM=ansi whiptail --title "Alist登录页" --infobox "http://127.0.0.1:5244" 8 40' > ~/.login-page.sh ; chmod +x ~/.login-page.sh) &>/dev/null
    (printf 'termux-open-url https://alist.nn.ci/zh/guide/drivers/ ; sleep 1 ; curl --silent --head --fail https://rimzone.github.io/#/rim/termuxalist > /dev/null 2>&1 && termux-open-url https://rimzone.neocities.org/pages/tutorial || termux-open-url https://rimzone.pages.dev/#/rim/termuxalist ; TERM=ansi whiptail --title "使用教程" --infobox "打开本软件自动启动Alist\n点击底部按钮控制本程序\n弹出的网页包含图文和视频详细用法说明" 12 40' > ~/.termux-alist-page.sh ; chmod +x ~/.termux-alist-page.sh) &>/dev/null
    (printf 'curl --silent --head --fail https://rimzone.github.io/#/rim/player > /dev/null 2>&1 && termux-open-url https://rimzone.github.io/#/rim/player || termux-open-url https://rimzone.pages.dev/#/rim/player ; TERM=ansi whiptail --title "WebDav播放器" --infobox "弹出的网页包含多种WebDav播放器下载" 12 40' > ~/.player-page.sh ; chmod +x ~/.player-page.sh) &>/dev/null
    (printf '~/.clear.sh ; curl --silent --head --fail http://127.0.0.1:5244 > /dev/null 2>&1 && TERM=ansi whiptail --title "Alist状态" --infobox "Alist已启动\n\n初始账号:admin\n初始密码:123" 12 40 || TERM=ansi whiptail --title "Alist状态" --infobox "Alist未启动！" 8 40' > ~/.info.sh ; chmod +x ~/.info.sh) &>/dev/null
    (printf '~/.login-page.sh ; ~/.info.sh' > ~/.firstrun.sh ; chmod +x ~/.firstrun.sh) &>/dev/null
    (printf 'clear && tmux clear-history' > ~/.clear.sh ; chmod +x ~/.clear.sh) &>/dev/null
    echo "[▆▆▆▆▆▆▆▆  ]下载自制脚本..."
    (curl -o ~/.ip.sh https://raw.gitcode.com/dazazi/alist/raw/main/mysh/ip.sh && chmod +x ~/.ip.sh) &>/dev/null
    (curl -o ~/.termux/termux.properties https://raw.gitcode.com/dazazi/alist/raw/main/mysh/termux.properties) &>/dev/null
    (curl -o $PREFIX/etc/motd https://raw.gitcode.com/dazazi/alist/raw/main/mysh/motd) &>/dev/null
    echo "[▆▆▆▆▆▆▆▆  ]开启自启功能..."
    (echo 'if [ -z "$TMUX" ]; then tmux new-session -d -s main -n alist_window ; tmux send-keys -t main:alist_window "alist server" C-m ; tmux new-window -t main -n gui ; tmux send-keys -t main:gui "clear ; echo "Alist正在启动..." ; sleep 1 ; ~/.starting.sh ; sleep 3 ; ~/.firstrun.sh ; > ~/.firstrun.sh ; ~/.info.sh" C-m ; tmux attach-session -t main ; fi' > ~/.bashrc) &>/dev/null
    echo "[▆▆▆▆▆▆▆▆  ]设置默认密码为123..."
    alist admin set 123 &>/dev/null
    echo "[▆▆▆▆▆▆▆▆▆ ]启动Alist和窗口设置..."
    chmod +x ~/.bashrc &>/dev/null
    echo "[▆▆▆▆▆▆▆▆▆▆]刷新配置..."
    termux-reload-settings &>/dev/null
    sleep 2
}

function update() {
    echo "执行更新流程..."
}

# 参数检查
if [ $# -ne 1 ]; then
    echo "错误：需要一个参数，但收到了 $# 个参数。"
    echo "使用方法: $(basename "$0") [install|update]"
    exit 1
fi

# 调用函数
case $1 in
    install|update)
        $1
        ;;
    *)
        echo "错误：无效的参数 '$1'。"
        echo "使用方法: $(basename "$0") [install|update]"
        exit 1
        ;;
esac